using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;

using MakeIT.Framework.Data;
using MakeIT.Alf.Site.Entidades;

namespace MakeIT.Alf.Site.Negocios
{
	public partial class AlfDAO : DAO
	{

		public static CaracteristicaVO getCaracteristica(int? idCaracteristica, string caracteristica)
		{
			CaracteristicaVO caracteristicaVO = null;
			List<CaracteristicaVO> listCaracteristicaVOs = getCaracteristicas(idCaracteristica, caracteristica);
			if (listCaracteristicaVOs != null && listCaracteristicaVOs.Count > 0)
			{
				caracteristicaVO = listCaracteristicaVOs[0];
			}
			return caracteristicaVO;
		}

		public static CaracteristicaVO getCaracteristica(int? idCaracteristica)
		{
			return getCaracteristica(idCaracteristica, null);
		}

		public static List<CaracteristicaVO> getCaracteristicas()
		{
			return getCaracteristicas(null, null);
		}

		public static List<CaracteristicaVO> getCaracteristicas(int? idCaracteristica)
		{
			return getCaracteristicas(idCaracteristica, null);
		}

		public static List<CaracteristicaVO> getCaracteristicas(int? idCaracteristica, string caracteristica)
		{
			string where = "WHERE ";
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT car.idCaracteristica as car_idCaracteristica, car.caracteristica as car_caracteristica, car.Cadastro as car_Cadastro, car.CadastradoPor as car_CadastradoPor, car.Edicao as car_Edicao, car.EditadoPor as car_EditadoPor  ");
			query.AppendLine("FROM caracteristica as car ");
			if (idCaracteristica != null)
			{
				query.AppendLine(where + "car.idCaracteristica=@idCaracteristica ");
				where = "AND ";
			}
			if (caracteristica != null)
			{
				query.AppendLine(where + "car.caracteristica LIKE @caracteristica ");
				where = "AND ";
			}


			query.AppendLine("ORDER BY car.caracteristica ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			if (idCaracteristica!= null) cmd.Parameters.Add(getParameter(cmd, "@idCaracteristica", idCaracteristica));
			if (caracteristica!= null) cmd.Parameters.Add(getParameter(cmd, "@caracteristica", "%"+caracteristica+"%"));


			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();

				List<CaracteristicaVO> listCaracteristica = new List<CaracteristicaVO>();

				while (reader.Read())
				{
					CaracteristicaVO caracteristicaVO = CaracteristicaVO.CreateVO();
					caracteristicaVO.idCaracteristica = reader.GetInt32(reader.GetOrdinal("car_idCaracteristica"));
					caracteristicaVO.caracteristica = reader.GetString(reader.GetOrdinal("car_caracteristica"));
					caracteristicaVO.cadastro = reader.GetDateTime(reader.GetOrdinal("car_Cadastro"));
					caracteristicaVO.cadastradoPor = reader.GetString(reader.GetOrdinal("car_CadastradoPor"));
					caracteristicaVO.edicao = reader.GetDateTime(reader.GetOrdinal("car_Edicao"));
					caracteristicaVO.editadoPor = reader.GetString(reader.GetOrdinal("car_EditadoPor"));


					listCaracteristica.Add(caracteristicaVO);
				}
				reader.Close();
				return listCaracteristica;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
                if (cmd != null && cmd.Connection != null)
                    cmd.Connection.Close();
            }
		}

		public static void setCaracteristica(CaracteristicaVO caracteristicaVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("UPDATE caracteristica ");
			query.AppendLine("SET caracteristica=@caracteristica,  Cadastro=@Cadastro, CadastradoPor=@CadastradoPor, Edicao=@Edicao, EditadoPor=@EditadoPor ");
			query.AppendLine("WHERE idCaracteristica=@idCaracteristica ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idCaracteristica", caracteristicaVO.idCaracteristica));
			cmd.Parameters.Add(getParameter(cmd, "@caracteristica", caracteristicaVO.caracteristica));
			cmd.Parameters.Add(getParameter(cmd, "@Cadastro", caracteristicaVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@CadastradoPor", caracteristicaVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@Edicao", caracteristicaVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@EditadoPor", caracteristicaVO.editadoPor));

			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }
            finally
            {
                if (cmd != null && cmd.Connection != null)
                    cmd.Connection.Close();
            }

		}

		public static long newCaracteristica(CaracteristicaVO caracteristicaVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("INSERT INTO caracteristica ");
			query.AppendLine("  ( caracteristica, Cadastro, CadastradoPor, Edicao, EditadoPor ) ");
			query.AppendLine("VALUES ( @caracteristica, @Cadastro, @CadastradoPor, @Edicao, @EditadoPor ) ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@caracteristica", caracteristicaVO.caracteristica));
			cmd.Parameters.Add(getParameter(cmd, "@Cadastro", caracteristicaVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@CadastradoPor", caracteristicaVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@Edicao", caracteristicaVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@EditadoPor", caracteristicaVO.editadoPor));


			try
			{
				cmd.ExecuteNonQuery();

				long newID = 0;
				if (tipo.Equals(DBType.MSSql))
				{
					cmd.CommandText = "SELECT IDENT_CURRENT('caracteristica') AS 'Identity' ";
					DbDataReader reader = cmd.ExecuteReader();
					reader.Read();
					newID = (long)reader.GetDecimal(reader.GetOrdinal("Identity"));
					reader.Close();
				}
				else if (tipo.Equals(DBType.MySql))
				{
					newID = ((MySql.Data.MySqlClient.MySqlCommand)cmd).LastInsertedId;
				}
				else if (tipo.Equals(DBType.ODBC))
				{
					//TODO implementar
				}
				else if (tipo.Equals(DBType.ORACLE))
				{
					//TODO implementar
				}
				return newID;
			}
			catch (Exception ex) { throw ex; }
            finally
            {
                if (cmd != null && cmd.Connection != null)
                    cmd.Connection.Close();
            }

		}

		public static void delCaracteristica(CaracteristicaVO caracteristicaVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("DELETE FROM caracteristica ");
			query.AppendLine("WHERE idCaracteristica=@idCaracteristica ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idCaracteristica", caracteristicaVO.idCaracteristica));


			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }
            finally
            {
                if (cmd != null && cmd.Connection != null)
                    cmd.Connection.Close();
            }

		}
	}
}
